<template>
    <div>
        <div style='background: #fff;padding:50px 50px 5px 50px'>
            <div class='content-head'>邮件配置</div>
            <Form ref="form" style="margin-top: 20px;width:50%" :label-width="110">
                <Form-item label="主机">
                    <p slot='label'><span class='star'>*</span>主机</p>
                    <Input v-model="form.host" placeholder="请填写主机"></Input>
                </Form-item>
                <Form-item label="SSL">
                    <p slot='label'><span class='star'>*</span>SSL</p>
                    <RadioGroup v-model="form.secureConnection">
                        <Radio label="1">是</Radio>
                        <Radio label="0">否</Radio>
                    </RadioGroup>
                </Form-item>
                <Form-item label="SMTP端口">
                    <p slot='label'><span class='star'>*</span>SMTP端口</p>
                    <Input v-model="form.port" placeholder="请填写SMTP端口"></Input>
                </Form-item>
                <Form-item label="登录账号">
                    <p slot='label'><span class='star'>*</span>登录账号</p>
                    <Input v-model="form.user" placeholder="请填写邮箱登录账号"></Input>
                </Form-item>
                <Form-item label="授权码">
                    <p slot='label'><span class='star'>*</span>授权码</p>
                    <Input v-model="form.pass" placeholder="请填写QQ邮箱的授权码"></Input>
                </Form-item>
                <Form-item>
                    <div class='btn btn-hover' @click='submit'>保存</div>
                </Form-item>
            </Form>
        </div>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                form: {
                    host:'',
                    secureConnection: 1,
                    port: '',
                    user:'',
                    pass:''
                },
                
            }
        },
        created(){
            this.getConfig();
        },
        methods: {
            getConfig(){
                this.fetch('/home/app/plugins/mail').then((res) => {
                    if (res && res.data.data) {
                        let data = res.data.data;
                        this.form = data;
                    }
                });
            },
            submit(){
                if(!this.form.host){
                    this.$Message.warning('请填写主机')
                    return
                }
                if(!this.form.port){
                    this.$Message.warning('请填写SMTP端口')
                    return
                }
                if(!this.form.user){
                    this.$Message.warning('请填写邮箱登录账号')
                    return
                }
                if(!this.form.pass){
                    this.$Message.warning('请填写QQ邮箱的授权码')
                    return
                }
                let data = {
                    host:this.form.host,
                    secureConnection:this.form.secureConnection,
                    port:this.form.port,
                    user:this.form.user,
                    pass:this.form.pass
                }
                if(this.form.id){
                    data.id = this.form.id;
                }
                
                this.fetch('/home/app/plugins/mail/add', data).then((res) => {
                    if (res && res.data.data) {
                        this.$Message.success('保存成功')
                        this.getConfig();
                    }
                });
            }
            
        }
    }
</script>